package net.steam.easyjoboa.biz.impl;

import java.util.ArrayList;
import java.util.List;

import net.steam.easyjoboa.biz.EmployeeBiz;
import net.steam.easyjoboa.dao.EmployeeDao;
import net.steam.easyjoboa.entity.Employee;
import net.steam.easyjoboa.entity.PageBean;
import net.steam.easyjoboa.entity.PageBeans;

public class EmployeeBizImpl implements EmployeeBiz {

	private EmployeeDao employeeDao;
	
	public Employee doLogin(String name, String password) {
		String hql=String.format("from Employee e where e.userName='%s' and e.userPassword='%s' ",name,password);
		List<Employee> list=employeeDao.find(hql);
		if(list.size()!=0){
			return list.get(0);
		}else{
			return null;
		}
	}
	
	public Integer addEmployee(Employee employee) throws Exception {
		employeeDao.addEntity(employee);
		return employee.getUserId();
	}

	public void updateEmployee(Employee employee) {
		employeeDao.updateEntity(employee);
	}

	public void delEmployee(Integer id) {
		try {
			employeeDao.deleteEntity(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Employee findEmployeeById(Integer id) {
		return employeeDao.findById(id);
	}

	public List<Employee> findAllEmployee(String propertyName, Object value) {
		String hql = "From Employee as e ";
		if(propertyName!=null){
			hql+="where e."+propertyName+" = "+value;
		}
		List<Employee> employees = employeeDao.find(hql);
		return employees;
	}
	
	/**
	 * 根据pagebean查找分页
	 */
	public List<Employee> findEmpByPage(PageBean pageBean,Employee employee) {
		List<Object> params=new ArrayList<Object>();
		List<Employee> employees=null;
		String hql="from Employee e where 1=1";
		if(employee!=null){
			//如果部门ID不等于空且不等于0，就证明选择了部门这个条件
			if(employee.getPosition().getDepartment().getDepartmentId()!=null&&employee.getPosition().getDepartment().getDepartmentId()!=0){
				hql+=" and e.position.department.departmentId=?";
				params.add(employee.getPosition().getDepartment().getDepartmentId());
			}
			//如果职位ID不等于空且不等于0，就证明选择了职位这个条件
			if(employee.getPosition().getPositionId()!=null&&employee.getPosition().getPositionId()!=0){
				hql+=" and e.position.positionId=?";
				params.add(employee.getPosition().getPositionId());
			}
			//判断是否有名字这个条件
			if(employee.getUserName()!=null&&!employee.getUserName().equals("")){
				hql+=" and e.userName=?";
				params.add(employee.getUserName());
			}
			 employees=employeeDao.find(pageBean, hql,params.toArray());
		}else{
			employees=employeeDao.find(pageBean, hql);
		}
		return employees;
	}
	
	public List<Employee> finEmployeesByProperty(Integer loginId,String userName,
			Integer userId, Integer positionId, Integer departmentId) {
		String hql = "From Employee as e where 1=1 and e.userJobstatus = 0 and e.userId!= "+loginId;
		List<Object> obj = new ArrayList<Object>();
		List<Employee> employees = null;
		if(userName!=null&&!userName.equals("")){
			hql += " and e.userName = ?";
			obj.add(userName);
		}
		if(userId!=null&&userId!=0){
			hql += " and e.userId = " +userId;
		}
		if(positionId!=null&&positionId!=0){
			hql += " and e.position.positionId =?";
			obj.add(positionId);
		}
		if(departmentId!=null&&departmentId!=0){
			hql += " and e.position.department.departmentId =?";
			obj.add(departmentId);
		}
		if(obj.size()>0){
			employees = employeeDao.find(hql, obj.toArray());
		}else{
			employees = employeeDao.find(hql);
		}
		return employees;
	}

	//根据部门Id查询员工进行投票
	public List<Employee> getEmployeesByDepartmentId(PageBeans pageBean,int id) {
		String hql ="from Employee where position.department.departmentId="+id;		
		List<Employee> list= employeeDao.findByPage(pageBean, hql);
		return list;
	}
	//得到所有的部门员工
	public List<Employee> getEmployeesList(int depId) {
		String hql = "from Employee where position.department.departmentId="+depId;
		return employeeDao.find(hql);
	}	


	public Employee getEmployeeById(int id) {
		return employeeDao.findById(id);
		}

	public List<Employee> findEmployees(PageBeans pageBeans, Employee employee) {
			List<Object> params = new ArrayList<Object>();
			List<Employee> list = new ArrayList<Employee>();
			String hql = "from Employee e where e.userJobstatus = 0 ";
			if(employee!=null){
				if (employee.getUserName().length()>0 && employee.getUserName()!=null ) {
					hql += " and e.userName=?";
					params.add(employee.getUserName());
				}
				if (employee.getPosition().getDepartment().getDepartmentId()!= null && employee.getPosition().getDepartment().getDepartmentId()>0) {
					hql +=" and e.position.department.departmentId=?";
					params.add(employee.getPosition().getDepartment().getDepartmentId());
				}
			}
			list = employeeDao.findByPage(pageBeans, hql, params.toArray());
			return list;
		}

	public List<Employee> getAllEmployeesExceptLoginUserId(int id) {		
		return employeeDao.find("from Employee e where e.userJobstatus = 0 and e.userId!=" + id);
	}
	
	
	public void setEmployeeDao(EmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}
}
